% main.m
% MATLAB 版本的主执行脚本。
% (版本 28: 修正了 calculate_total_time 的调用)

clear; clc; close all;

% --- 1. 设置参数 ---
case_name = 'MatMul_Case1'; % 改为当前要测试的案例
fprintf('--- 正在处理算例: %s ---\n', case_name);

% --- 2. 加载计算图和调度文件 ---
nodes_file = [case_name, '_Nodes.csv'];
edges_file = [case_name, '_Edges.csv'];
[graph, code_map] = load_graph(nodes_file, edges_file);
fprintf('计算图加载完成。\n');
p1_schedule_file = [case_name, '_final_schedule_optimized.txt'];
p1_schedule = load_schedule_from_file(p1_schedule_file);
fprintf('问题1调度结果加载完成，共 %d 个节点。\n', length(p1_schedule));

% --- 3. 执行问题2：缓存分配 ---
fprintf('\n--- 开始求解问题2: 缓存分配 ---\n');
l1_cap = 4096;
ub_cap = 1024;
l0a_cap = 256;
l0b_cap = 256;
l0c_cap = 512;
[solution, graph] = perform_allocation(p1_schedule, graph, code_map, l1_cap, ub_cap, l0a_cap, l0b_cap, l0c_cap, 0, 0);
fprintf('问题2求解完成。\n');

% --- 4. 计算评估指标 ---
fprintf('\n--- 求解结果 ---\n');
fprintf('总额外数据搬运量 (Total Spill Volume): %d\n', solution.total_spill_volume);

% *** 已修正: 增加了 code_map 作为输入参数 ***
total_execution_time = calculate_total_time(solution, graph, code_map);
fprintf('总执行时间 (Total Execution Time): %d cycles\n', total_execution_time);

% --- 5. 最终验证 ---
fprintf('\n--- 开始最终验证 ---\n');
is_valid = validate_solution(solution, graph, code_map, l1_cap, ub_cap, l0a_cap, l0b_cap, l0c_cap);
if is_valid
    fprintf('验证通过: 最终解决方案满足所有约束。\n');
else
    fprintf('!!! 验证失败: 最终解决方案存在问题 !!!\n');
end

% --- 6. 保存结果 ---
fprintf('\n--- 开始保存结果 ---\n');
output_dir = 'Problem2_MATLAB';
if ~exist(output_dir, 'dir')
   mkdir(output_dir);
end
write_schedule_to_file(fullfile(output_dir, [case_name, '_schedule.txt']), solution.final_schedule);
write_memory_to_file(fullfile(output_dir, [case_name, '_memory.txt']), solution.memory_offsets);
spill_log_for_file = format_spill_log_for_output(solution.spill_map, graph);
write_spill_to_file(fullfile(output_dir, [case_name, '_spill.txt']), spill_log_for_file);
fprintf('所有结果已成功保存至 ./%s 目录。\n', output_dir);

% --- 7. 可视化 ---
fprintf('\n--- 开始生成可视化图表 ---\n');
history = run_simulation_for_viz(solution, graph, code_map, l1_cap, ub_cap, l0a_cap, l0b_cap, l0c_cap);
target_type_code = code_map.L1;
target_capacity = l1_cap;

% 创建可视化输出目录
viz_dir = fullfile(output_dir, 'visualizations');
if ~exist(viz_dir, 'dir')
    mkdir(viz_dir);
end

% 生成并保存可视化图表
fig1 = visualize_fragmentation(history, target_type_code);
set(fig1, 'ToolBar', 'none'); % 移除工具栏
saveas(fig1, fullfile(viz_dir, [case_name, '_fragmentation.png']));
saveas(fig1, fullfile(viz_dir, [case_name, '_fragmentation.fig']));

fig2 = visualize_gantt_chart(history, target_type_code, target_capacity);
set(fig2, 'ToolBar', 'none'); % 移除工具栏
saveas(fig2, fullfile(viz_dir, [case_name, '_gantt_chart.png']));
saveas(fig2, fullfile(viz_dir, [case_name, '_gantt_chart.fig']));

fig3 = visualize_buffer_lifetimes(history, target_type_code);
set(fig3, 'ToolBar', 'none'); % 移除工具栏
saveas(fig3, fullfile(viz_dir, [case_name, '_buffer_lifetimes.png']));
saveas(fig3, fullfile(viz_dir, [case_name, '_buffer_lifetimes.fig']));

fprintf('可视化图表已生成并保存至 %s 目录。\n', viz_dir);